<?php
/**
 * Created by PhpStorm.
 * User: jsq
 * Date: 2017/12/5 0005
 * Time: 13:40
 */

namespace app\admin\controller;

use app\admin\logic\Excel as Excelapi;
use app\admin\logic\Order as order_logic;
use app\admin\model\Order;
use app\admin\model\Order_log;
use app\common\controller\Base;
use think\Db;
use app\common\traits\Page;
use think\Request;
use app\common\traits\Common;
use app\common\model\Leibie;

class Daoru extends Admin
{
    use page;
    /**
     * 导入客资错误列表
     * @return mixed
     */
    public function index(){
//        $Excel_logic = new Excel_logic();
//        $Excel = $Excel_logic->getAllExcelToTree();
//        $this->assign('Excel', $Excel);
        $order = new Order_logic();
        $data['status'] = ['eq', '1'];
        $data = $order->power($data);
        $res = $this->pageshow('order_error', $data, [], $order = 'id desc');
        $this->assign("list", $res);
        $this->getDatas();
        return $this->fetch();
    }

    /**
     * 导入excel 渠道导入客资
     * @return mixed
     */
    public function daoru(){
//        $text = "<span style='color: red;'>待导入数据！</span>";
//        $file = request()->file('excel');
//        if ($file) {
//            $info = $file->move('public/'. 'uploads/');
//            $file_name = 'public/uploads/'.$info->getSaveName();
//            $arr = Excel::daorukezi('./' . $file_name);
//            unlink('.' . $file_name);
////            $arr = Excel::daoruDataToQueue($file_name);
//            $text = empty($arr)?'<span style=\'color: red;\'>全部导入成功！</span>':'';
//            unlink($file_name);
//        }
//        $this->assign('info', $arr);
//        $this->assign('text', $text);
        return $this->fetch() ;
    }

    /**
     *渠道导入客资
     */
    public function daorus()
    {
//        ini_set('memory_limit','2048M');
//        $objReader = \PhpOffice\PhpSpreadsheet\IOFactory::load('./uploads/file/20180210/1.xlsx');
//        halt($objReader);
        $input = $this->request->post('fileurl');
        $reult = Excelapi::daorukezi('.' . $input);
        unlink('.' . $input);
        if ($reult['code'] == 1) {
            $this->success($reult['msg']);
        } else {
            $this->error($reult['msg']);
        }
    }
    
//    /**
//     * 添加导入
//     * @return mixed
//     */
//    public function add()
//    {
//        $order = new Order();
//        if ($this->request->isPost()) {
//            $data = $this->request->post();
//            $re = $order->edit($data, $khd = 1);//成功返回1
//            return $re;
//        } else {
//            $this->getSj();
//            return $this->getAddMoban();
//        }
//    }
//
//    /**
//     * 删除导入
//     */
//    public function del()
//    {
//        $id = $this->request->param('id', 0);
//        $Excel_logic = new Excel_logic();
//        $result = $Excel_logic->delExcel($id);
//        if ($result['result'] === true) {
//            $this->success($result['msg']);
//        } else {
//            $this->error($result['msg']);
//        }
//    }


    /**
     *导出excel
     */
//    public function daochu(){
//        $map = request()->param();
//        ini_set('memory_limit', '1024M');
//        dump($map);
//    }
    public function daochu(Request $request){
        ini_set('memory_limit', '2048M');
        $excel = new Excelapi;
        $order = new Order();
        $leibie = new Leibie();
        $map = $request->param();
        $map = $this->queryData($map);
        $map = $this->checkData($map);
//        $data = Db::name('order')->where($map)->cache(600)->field('id,name_z,name_f,phone_z,phone_f,channel,jdry,state')->select();
        $data = $order->where($map)->cache(600)->where('status',1)
            ->field('id,name_z,name_f,phone_z,phone_f,channel,source,jdtime,jdry,state,yykf,yybz,bbcstime,track')
            ->select();
        $title = ['ID', '主联系人', '副联系人', '主电话', '副电话','渠道','来源','建单时间','建单人员','订单状态','邀约客服','邀约备注','宝宝生日','追踪备注'];
        foreach ($data as $k=>$v){
            $datas[$k][] = $v['id'];
            $datas[$k][] = $v['name_z'];
            $datas[$k][] = $v['name_f'];
            $datas[$k][] = $v['phone_z'];
            $datas[$k][] = $v['phone_f'];
            $datas[$k][] = $v['channel_text'];
            $datas[$k][] = $v['source_text'];
            $datas[$k][] = $v['jdtime'] != '1970-01-01'?$v['jdtime']:'';
            $datas[$k][] = $v['jdry_text'];
            $datas[$k][] = $leibie->getState($v['state']);
            $datas[$k][] = $v['yykf_text'];
            $datas[$k][] = clearRichText($v['yybz']);
            $datas[$k][] = $v['bbcstime'] != '1970-01-01'?$v['bbcstime']:'';
            $datas[$k][] = clearRichText($v['track_text']);
        }
//        halt($datas);
        $arr = $excel->exportexcel($datas,$title);
        if ($arr['result'] === true) {
            $this->success($arr['msg']);
        } else {
            $this->error($arr['msg']);
        }
    }
}